<?php
namespace app\command;

use think\console\Command;
use think\console\Input;
use think\console\input\Option;
use think\console\Output;
use think\Db;
use think\Exception;

/**
 * Class Profit
 * @package app\command
 */
class Bouns extends Command
{
    protected $contractHelper = null;
    protected  $mtHelper =null;
    protected function configure()
    {
           $this->setName('BounseProfit')
               ->setDescription('分红');
    }


    protected function execute(Input $input, Output $output) {
        $order_list= Db::table('tb_order')->where(array("pay_status"=>1,'status'=>1,'deal'=>0))->limit(100)->select();
        if (!empty($order_list)){
            foreach ($order_list as $item){
                $card = $item["card"];
                $obj = Db::table("tb_card_info")->field("info.*,agent.bonus as agent,agent2.bonus as agent2,shop.bonus as shop")->alias("info")->order('id asc');
                $obj->join('tb_card_shop agent',"info.agent_id=agent.id","left");
                $obj->join('tb_card_shop shop',"info.shop_id=shop.id","left");
                $obj->join('tb_card_shop agent2',"info.agent2_id=agent2.id","left");
                 $cardInfo =  $obj->where("card",$card)->find();

                Db::startTrans();
                try{
                    echo json_encode($item)."\n";;

                    Db::table('tb_order')->where('id',$item['id'])->update(array('deal'=>1)); //更新订单状态

                    if ($cardInfo["agent"]){
                        $money1 = round($cardInfo["agent"]*$item["amount"],2);
                        Db::table('tb_shop_bouns_record')->insert([
                            "shop_id"=>$cardInfo["agent_id"],
                            "bouns"=>$cardInfo["agent"],
                            "order_id"=>$item["order_id"],
                            "money"=>$money1,
                            "card"=>$item["card"],
                            "time"=>time(),
                            "t_money"=>$item["amount"]
                        ]);

                        Db::table('tb_card_shop')->where("id",$cardInfo["agent_id"])->setInc("money",$money1);
                    }
                    if ($cardInfo["agent2"]){
                        $money2 = round($cardInfo["agent2"]*$item["amount"],2);
                        Db::table('tb_shop_bouns_record')->insert([
                            "shop_id"=>$cardInfo["agent2_id"],
                            "bouns"=>$cardInfo["agent2"],
                            "order_id"=>$item["order_id"],
                            "money"=>$money2,
                            "card"=>$item["card"],
                            "time"=>time(),
                            "t_money"=>$item["amount"]
                        ]);

                        Db::table('tb_card_shop')->where("id",$cardInfo["agent2_id"])->setInc("money",$money2);
                    }
                    if ($cardInfo["shop"]){
                        $money3 = round($cardInfo["shop"]*$item["amount"],2);
                        Db::table('tb_shop_bouns_record')->insert([
                            "shop_id"=>$cardInfo["shop_id"],
                            "bouns"=>$cardInfo["shop"],
                            "order_id"=>$item["order_id"],
                            "money"=>$money3,
                            "card"=>$item["card"],
                            "time"=>time(),
                            "t_money"=>$item["amount"]
                        ]);
                        Db::table('tb_card_shop')->where("id",$cardInfo["shop_id"])->setInc("money",$money3);
                    }



                    Db::commit();
                    echo 'ok'."\n";;
                } catch (\Exception $e) {
                    Db::rollback();
                    echo 'no:'.$e->getMessage()."\n";
                    continue;
                }
            }

        }
    }


}
